package com.stedu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stedu.bean.Games;
import com.stedu.bean.RespBean;
import com.stedu.exception.SteduException;
import com.stedu.group.UpdateGroup;
import com.stedu.service.GamesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.validation.constraints.NotNull;
import javax.validation.groups.Default;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping( "/admin/GamesController")

public class GamesController  {
    @Autowired
    private GamesService gamesService;

    @PostMapping
    public RespBean addEmployee(@RequestBody @Validated Games games) throws ServletException, IOException {


        //添加

            gamesService.addemp(games);
            return RespBean.ok("添加成功");

    }
    @RequestMapping ("/{gid}")
    public RespBean deleteEmp(@PathVariable("gid") Long gid)  {

            gamesService.delete(gid);
            return RespBean.ok("修改成功");

    }
    @GetMapping("/page")
    public RespBean findbypage(@NotNull Integer pagecou, String gname, String gnum)  {
        System.out.println("=========");
        PageHelper.startPage(pagecou, 5);
        List<Games> games = gamesService.selecyAll(gname, gnum);
        PageInfo<Games> gamesPageInfo = new PageInfo<>(games);
        return RespBean.ok("", gamesPageInfo);

    }
    @PutMapping
    public RespBean upemployee(@RequestBody @Validated({UpdateGroup.class, Default.class}) Games games) throws  SteduException {

        games.setEstate(1);

        System.out.println(games);
        //添加


            gamesService.update(games);
            return RespBean.ok("修改成功");



    }
    @GetMapping("/{gid}")
    public RespBean findById(@PathVariable("gid") @NotNull Long gid) throws ServletException, IOException {


            Games games = gamesService.selectById(gid);
            return RespBean.ok("", games);


    }
    @GetMapping("/findAll")
    public RespBean findAll () {

            List<Games> selectall = gamesService.selecyAll (null,null);
            return RespBean.ok("", selectall);

    }
    @GetMapping("/findrevenue")
     public RespBean findrevenue () {

            List<Map<String, Object>> findrevenue = gamesService.findrevenue();
            return RespBean.ok("",findrevenue);

    }
}
